
/*
 * sndpipe: A UNIX pipes-based audio stream processor toolkit.
 * Copyright (C) 2011  Bradley Worley
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to:
 *
 *   Free Software Foundation, Inc.
 *   51 Franklin Street, Fifth Floor
 *   Boston, MA  02110-1301, USA.
 */

/* include the sndpipe header. */
#include "sndpipe.h"

/* func_txt: initiates the TXT functionality.
 *
 * this function opens up an input pipe (required) and converts all read
 * audio stream data into textual output, formatted as space-separated
 * values.
 */
void func_txt (void) {
  /* declare required variables. */
  unsigned long c, i, frames;
  struct pipes *pi;
  double **buf;

  /* attempt to open the input pipe. */
  pi = pipes_open_input ();

  /* ensure the input pipe was successfully opened. */
  if (!pi) {
    /* print a failure message and exit the application. */
    failf ("unable to open input pipe");
  }

  /* allocate the pipes buffer memory. */
  frames = 0;
  buf = pipes_buf_alloc (pi, &frames);

  /* loop while we can read samples from the pipe. */
  while (pipes_read (pi, &buf, frames) == SP_OK) {
    /* loop through the number of data frames. */
    for (i = 0; i < frames; i++) {
      /* and through the number of input channels, printing values. */
      for (c = 0; c < pi->sfi.channels; c++)
        fprintf (stdout, "%16lf ", buf[c][i]);

      /* finish every frame with a newline. */
      fprintf (stdout, "\n");
    }
  }

  /* free the input pipes buffer memory. */
  pipes_buf_free (pi, frames, buf);

  /* attempt to close the input pipe. */
  if (pipes_close (pi) != SP_OK) {
    /* print a failure message and exit the application. */
    failf ("unable to close input pipe");
  }
}

